#include "yanzhengdenglu.h"
#include "ui_yanzhengdenglu.h"

#include "YanZhengMa.h"
#include <QLineEdit>
#include "sql.h"
#include "config.h"

YanZhengDengLu::YanZhengDengLu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::YanZhengDengLu)
{
    ui->setupUi(this);

    // 创建一个标签用于显示背景图像
        QLabel *backgroundLabel = new QLabel(this);
        QPixmap backgroundImage(":/new/prefix1/bjt.png"); // 加载背景图像
        backgroundLabel->setPixmap(backgroundImage);
        backgroundLabel->setScaledContents(true); // 缩放背景图像以填充标签

        // 设置标签的位置和大小以覆盖整个窗口
        backgroundLabel->setGeometry(0, 0, this->width(), this->height());

        // 将标签放置在窗口的底层
        backgroundLabel->lower();


}

YanZhengDengLu::~YanZhengDengLu()
{
    delete ui;
}

void YanZhengDengLu::on_fasongyanzhengma_clicked()
{
    QLineEdit *EmailEdit = findChild<QLineEdit*>("youxiang");

    QString Email = EmailEdit->text();
    qDebug()<<Email;

    QString captcha = EmailService::generateCaptcha();
    QString recipient = Email; // 收件人邮箱地址
    if (EmailService::sendCaptchaEmail(recipient, captcha)) {
        qDebug() << "验证码发送成功.";
    } else {
        qDebug() << "验证码发送失败.";
    }

    correctCode = captcha; // 将生成的验证码保存到 correctCode 变量中
}


void YanZhengDengLu::on_yanzhengmadenglu_clicked()
{

        QLineEdit *EmailEdit = findChild<QLineEdit*>("youxiang");
        QLineEdit *CodeEdit = findChild<QLineEdit*>("yanzhengma");

        QString Email = EmailEdit->text();
        QString Code = CodeEdit->text();

        // 假设这里有一个保存正确验证码的变量 correctCode，您需要在发送验证码时记录正确的验证码
        if (EmailService::verifyCode(Code, correctCode)) {
            //登录成功，显示主窗口
            QWidget *userView = new QWidget();
            userView->show();
            this->hide();
            // 验证码正确，允许登录
            qDebug() << "验证码正确，登录成功！";
        } else {
            // 验证码错误，登录失败
            qDebug() << "验证码错误，登录失败！";
        }

}


void YanZhengDengLu::on_fanhui_clicked()
{
    emit backToLogin(); // 发射返回登录界面的信号
}

